package com.piggy.logi.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.piggy.logi.sys.bo.LogiCarrierContractEditBo;
import com.piggy.logi.sys.domain.LogiCarrierContract;
import com.piggy.logi.sys.mapper.LogiCarrierContractMapper;
import com.piggy.logi.sys.service.ILogiCarrierContractService;
import com.piggy.logi.sys.vo.LogiCarrierContractVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 承运商合同Service业务层处理
 */
@Slf4j
@Service
public class LogiCarrierContractServiceImpl extends ServiceImpl<LogiCarrierContractMapper, LogiCarrierContract> implements ILogiCarrierContractService {

    @Override
    public List<LogiCarrierContractVo> getByCarrierId(Long carrierId) {
        return BeanUtil.copyToList(baseMapper.selectList(Wrappers.<LogiCarrierContract>lambdaQuery()
                .eq(LogiCarrierContract::getCarrierId, carrierId)), LogiCarrierContractVo.class);
    }

    @Override
    public List<LogiCarrierContractVo> getByCarrierIds(List<Long> carrierIds) {
        if (CollUtil.isEmpty(carrierIds)) {
            return Lists.newArrayList();
        }
        return BeanUtil.copyToList(baseMapper.selectList(Wrappers.<LogiCarrierContract>lambdaQuery()
                .in(LogiCarrierContract::getCarrierId, carrierIds)), LogiCarrierContractVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateContract(Long carrierId, List<LogiCarrierContractEditBo> carrierContractList) {
        if (CollUtil.isEmpty(carrierContractList)) {
            baseMapper.removeByCarrierId(carrierId);
            return;
        }
        List<LogiCarrierContract> carrierContracts = baseMapper.selectByCarrierId(carrierId);

        List<LogiCarrierContract> updates = BeanUtil.copyToList(carrierContractList.stream().filter(item -> item.getContractId() != null)
                .collect(Collectors.toList()), LogiCarrierContract.class);
        Set<Long> updateIds = updates.stream().map(LogiCarrierContract::getContractId).collect(Collectors.toSet());
        List<LogiCarrierContract> adds = BeanUtil.copyToList(carrierContractList.stream().filter(item -> item.getContractId() == null)
                .collect(Collectors.toList()), LogiCarrierContract.class);
        List<Long> removeIds = carrierContracts.stream().map(LogiCarrierContract::getContractId)
                .filter(contractId -> !updateIds.contains(contractId)).distinct().collect(Collectors.toList());

        this.updateBatchById(updates);
        this.saveBatch(adds);
        this.removeBatchByIds(removeIds);
    }
}
